package com.xuhuan.practice.excepition;

import com.xuhuan.practice.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: huan.xu
 * @create: 2024/6/10 18:42
 * @description: 全局异常处理
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

  @ExceptionHandler(Exception.class)
  public Result handleException(Exception e) {
    log.error("发现全局异常，错误信息:{}", ExceptionUtils.getStackTrace(e));
    return Result.error(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : "操作失败");
  }
}
